package br.com.unifor.dctree.dc;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DocumentCluster {

	private int nDocs;
	private Set<String> docIds;
	private Map<String,Double> features;
	
	public DocumentCluster(){
		
	}
	
	public DocumentCluster(int nDocs, Set<String> docIds, Map<String,Double> features){
		this.setnDocs(nDocs);
		this.setDocIds(docIds);
		this.setFeatures(features);
	}
	
	public DocumentCluster mergeClusters(DocumentCluster dc1, DocumentCluster dc2){
		DocumentCluster dcnew = new DocumentCluster();
		dcnew.setnDocs(dc1.getnDocs() + dc2.getnDocs());
		dcnew.setDocIds(mergeSets(dc1.getDocIds(), dc2.getDocIds()));
		dcnew.setFeatures(mergeMaps(dc1.getFeatures(), dc2.getFeatures()));
		return dcnew;
	}

	private Map<String, Double> mergeMaps(Map<String, Double> features1, Map<String, Double> features2) {
		Map<String, Double> features = new HashMap<String, Double>(features1);
		
		for (String feature : features2.keySet()) {
			Double featureValue = features.get(feature);
			if (featureValue != null) {
				featureValue += features2.get(feature);
			}
			else {
				featureValue = features2.get(feature);
			}
			
			features.put(feature, featureValue);
		}
		
		return features;
	}

	public int getnDocs() {
		return nDocs;
	}

	public void setnDocs(int nDocs) {
		this.nDocs = nDocs;
	}

	public Set<String> getDocIds() {
		return docIds;
	}

	public void setDocIds(Set<String> strings) {
		this.docIds = strings;
	}

	public Map<String, Double> getFeatures() {
		return features;
	}

	public void setFeatures(Map<String, Double> features) {
		this.features = features;
	}
	
	private Set<String> mergeSets(Set<String> set, Set<String> set2){
		Set<String> result = null;
		for(String s:set){
			result.add(s);
		}
		for(String s:set2){
			result.add(s);
		}
		return result;
	}
	
	public int compareTo(DocumentCluster dc){
		//Compare DCs with Manhattan Distance
		return 0;
	}
}
